;(function() {
	'use strict';
	angular.module('services.omdisplay.operationDevelop', [])
		.factory('odOperationDevelop', odOperationDevelop);

	function odOperationDevelop(CoreService) {
		return {
			fmtSendData: function(sendData) {
				var send = {
					startTime: '',
					endTime: '',
					system: [],
					systemName: [],
					business: [],
					businessName: [],
					channelCode: [],
					channelName: [],
					timeType: '1'
				};
				send.business = sendData.business.map(function(b) {
					return b.businessCode;
				});
				send.businessName = sendData.business.map(function(b) {
					return b.businessName;
				});
				send.channelCode = sendData.channel.map(function(c) {
					return c.businessLineCode;
				});
				send.channelName = sendData.channel.map(function(c) {
					return c.businessLineName;
				});
				if (sendData.system === '') {
					send.system.length = 0;
				} else {
					send.system.push(sendData.system.systemCode);
					send.systemName.push(sendData.system.systemName);
				}
				send.timeType = sendData.timeType;
				if (sendData.timeType === '1') {
					send.startTime = CoreService.dateFormat(sendData.startTime, 'yyyy-MM-dd');
					send.endTime = CoreService.dateFormat(sendData.endTime, 'yyyy-MM-dd');
				} else {
					send.startTime = CoreService.dateFormat(sendData.startTime, 'yyyy-MM');
					send.endTime = CoreService.dateFormat(sendData.endTime, 'yyyy-MM');
				}
				return send;
			},
			getEchartsData: function(items, title, echartsXName, echartsLegendName, echartsDataName,echartsType) {
				var seriesObj = {};
				var seriesData = {};
				var temp = {
					echartsXData: [],
					echartsLegend: [],
					echartsSeries: [],
					echartsTitleText: title
				};
				angular.forEach(items, function(item) {
					if (temp.echartsXData.indexOf(item[echartsXName]) === -1) {
						temp.echartsXData.push(item[echartsXName]);
					}
					if (temp.echartsLegend.indexOf(item[echartsLegendName]) === -1) {
						temp.echartsLegend.push(item[echartsLegendName]);
					}
					seriesObj[item[echartsLegendName]] = [];
					seriesData[item[echartsLegendName]] = {};
				});
				angular.forEach(temp.echartsXData, function(x) {
					angular.forEach(items, function(item) {
						seriesData[item[echartsLegendName]][x] = 0;
					});
				});
				angular.forEach(items, function(item) {
					seriesData[item[echartsLegendName]][item[echartsXName]] = item[echartsDataName];
				});
				angular.forEach(seriesData, function(s, index) {
					angular.forEach(temp.echartsXData, function(x) {
						seriesObj[index].push(s[x]);
					});
				});
				angular.forEach(temp.echartsLegend, function(el, i) {
					temp.echartsSeries[i] = {
						name: el,
						type: echartsType,
						data: seriesObj[el]
					};
				});
				// var myEcharts = echarts.init(document.getElementById('doc'));
				var a = this.echartsExamples(temp);
				return a;
				// myEcharts.setOption(a);
			},
			echartsExamples: function(temp) {
				var option = {
					title: {
						text: temp.echartsTitleText,
						x: 'center'
					},
					animation: false,
					dataZoom: {
						show: true,
						realtime: true
					},
					grid: {
						x2: 100
					},
					tooltip: {
						show: true
					},
					legend: {
						show: true,
						orient: 'vertical',
						data: temp.echartsLegend,
						x: 'right',
						y: 'center'
					},
					xAxis: [{
						type: 'category',
						data: temp.echartsXData
					}],
					yAxis: [{
						type: 'value',
						boundaryGap: [0, 0.1]
					}],
					series: temp.echartsSeries
				};
				return option;
			},
			getTitle: function(send, modelName) {
				var title = {};
				send.systemName.length === 0 ? title.systemName = '' : title.systemName = send.systemName.toString();
				if (send.startTime === send.endTime) {
					title.startTime = send.startTime.replace(/-/g, '');
					return title.systemName + title.startTime + send.businessName.join(',') + '业务' + send.channelName.join(',') + '渠道' + modelName;
				} else {
					title.startTime = send.startTime.replace(/-/g, '');
					title.endTime = send.endTime.replace(/-/g, '');
					return title.systemName + title.startTime + '_' + title.endTime + send.businessName.join(',') + '业务' + send.channelName.join(',') + '渠道' + modelName;
				}
			}
		};
	}
})();